package testJdk.anno.older;


import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @author keyer
 * @Description : 处理注解的类
 * 一般通过反射来获取注解，和注解上的信息
 * Created by cyd on 2021-04-29 14:21:22
 */
public class AnnotationAccess {
    /**
     * 打印类的注解信息
     *
     * @param cla
     */
    public static void printClass(Class cla) throws NoSuchMethodException {
        System.out.println("--------获取类上的注解------------");

        // 获取类上的注解
        Annotation[] annotations = cla.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        System.out.println("---------获取注解上的信息-----------");
        if (cla.isAnnotationPresent(AuthorAnnotation.class)) {
            AuthorAnnotation aa = (AuthorAnnotation) cla.getAnnotation(AuthorAnnotation.class);
            System.out.println("作者：" + aa.name() + "," + "公司：" + aa.company());
        }

        System.out.println("---------获取方法上的注解-----------");
        Method method = cla.getDeclaredMethod("getName");
        Annotation[] annotations2 = method.getAnnotations();
        for (Annotation annotation : annotations2) {
            System.out.println(annotation);
        }
    }

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
        Class<?> aClass = Class.forName("testJdk.anno.older.Person");
        printClass(aClass);
    }
}
